namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class UserRegistWayBLL
    {
        public int Add(UserRegistWay model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserRegistWay(");
            builder.Append("WayName");
            builder.Append(") values (");
            builder.Append("@WayName");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@WayName", model.WayName) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int WayID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserRegistWay ");
            builder.Append(" where WayID=@WayID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@WayID", SqlDbType.Int, 4) };
            cmdParms[0].Value = WayID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string WayIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserRegistWay ");
            builder.Append(" where ID in (" + WayIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<UserRegistWay> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserRegistWay ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            UserRegistWay item = null;
            List<UserRegistWay> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserRegistWay>();
                foreach (DataRow row in table.Rows)
                {
                    item = new UserRegistWay();
                    if (row["WayID"].ToString() != "")
                    {
                        item.WayID = int.Parse(row["WayID"].ToString());
                    }
                    item.WayName = row["WayName"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public UserRegistWay GetModel(int WayID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select WayID, WayName  ");
            builder.Append("  from UserRegistWay ");
            builder.Append(" where WayID=@WayID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@WayID", SqlDbType.Int, 4) };
            cmdParms[0].Value = WayID;
            UserRegistWay way = new UserRegistWay();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["WayID"].ToString() != "")
                {
                    way.WayID = int.Parse(row["WayID"].ToString());
                }
                way.WayName = row["WayName"].ToString();
                return way;
            }
            return null;
        }

        public bool Update(UserRegistWay model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserRegistWay set ");
            builder.Append(" WayName = @WayName  ");
            builder.Append(" where WayID=@WayID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@WayID", model.WayID), new SqlParameter("@WayName", model.WayName) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

